package com.tv189.springbatchtest.stepchecktasklet;


import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.List;

/**
 * @ClassName: ChunkTaskletJob
 * @Description: 居于快的任务
 * @Author: 19202
 * @Date: 2025/5/21 20:39
 * @Version 1.0
 **/
@SpringBootApplication
@EnableBatchProcessing
public class ChunkTaskletJob {

    private final JobBuilderFactory jobBuilderFactory;

    private final StepBuilderFactory stepBuilderFactory;

    int timer = 10;

    public ChunkTaskletJob(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }

    @Bean
    public ItemReader<String> itemReader(){
        return new ItemReader<String>() {

            /**
             * @return
             * @throws Exception
             * @throws UnexpectedInputException
             * @throws ParseException
             * @throws NonTransientResourceException
             */
            @Override
            public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
                if (timer > 0) {
                    System.out.println("-------------read------------ ");
                    return "read-ret-->" + timer--;
                }else {
                    return null;
                }
            }
        };
    }

    @Bean
    public ItemProcessor<String, String> itemProcessor(){
        return item -> {
            System.out.println("-------------process------------>" + item);
            return "process-ret" + item;
        };
    }

    @Bean
    public ItemWriter<String> itemWriter() {
        return System.out::println;
    }

    /**
     * step1
     *
     * @return {@link Step }
     */
    @Bean
    public Step chunkStep() {
        return stepBuilderFactory
                .get("chunkStep")
                .<String, String> chunk(3)
                .reader(itemReader())
                .processor(itemProcessor())
                .writer(itemWriter())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("chunk-tasklet-job")
                .start(chunkStep())
                .incrementer(new RunIdIncrementer())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ChunkTaskletJob.class, args);
    }

}
